package com.ktgj.service.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.service.domain.OrderBoarding;
import com.ktgj.service.domain.OrderitemBoarding;
import com.ktgj.service.domain.vo.*;
import com.ktgj.service.service.IOrderitemBoardingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;

import com.ktgj.service.service.IOrderBoardingService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 值机订单Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/orderboarding")
public class OrderBoardingController extends BaseController
{
    @Autowired
    private IOrderBoardingService orderBoardingService;

    @Autowired
    private IOrderitemBoardingService orderitemBoardingService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询值机订单列表
     */
    //@PreAuthorize(hasPermi = "service:boarding:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderBoarding orderBoarding)
    {
        startPage();
        List<OrderBoarding> list = orderBoardingService.selectOrderBoardingList(orderBoarding);
        return getDataTable(list);
    }

    /**
     * 导出值机订单列表
     */
    @PreAuthorize(hasPermi = "service:boarding:export")
    @Log(title = "值机订单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderBoarding orderBoarding) throws IOException
    {
        List<OrderBoarding> list = orderBoardingService.selectOrderBoardingList(orderBoarding);
        ExcelUtil<OrderBoarding> util = new ExcelUtil<OrderBoarding>(OrderBoarding.class);
        util.exportExcel(response, list, "值机订单 数据");
    }

    /**
     * 获取值机订单详细信息
     */
    @PreAuthorize(hasPermi = "service:boarding:query")
    @GetMapping(value = "/{orderboardingId}")
    public AjaxResult getInfo(@PathVariable("orderboardingId") Long orderboardingId)
    {
        return AjaxResult.success(orderBoardingService.selectOrderBoardingById(orderboardingId));
    }

    /**
     * 新增值机订单
     */
    //@PreAuthorize(hasPermi = "service:boarding:add")
    @Log(title = "值机订单 ", businessType = BusinessType.INSERT)
    @PostMapping("/insertadd")
    public AjaxResult add(@RequestBody OrderBoarding orderBoarding)
    {
        int i = orderBoardingService.insertOrderBoarding(orderBoarding);
        if (i > 0){
            return AjaxResult.success("成功");
        }
        else {
            return AjaxResult.error("失败");
        }
    }

    /**
     * 修改值机订单
     */
    @PreAuthorize(hasPermi = "service:boarding:edit")
    @Log(title = "值机订单 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderBoarding orderBoarding)
    {
        return toAjax(orderBoardingService.updateOrderBoarding(orderBoarding));
    }

    /**
     * 删除值机订单
     */
    @PreAuthorize(hasPermi = "service:boarding:remove")
    @Log(title = "值机订单 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderboardingIds}")
    public AjaxResult remove(@PathVariable Long[] orderboardingIds)
    {
        return toAjax(orderBoardingService.deleteOrderBoardingByIds(orderboardingIds));
    }

    /**
     * 值机订单  查看详情
     * @param orderboardingId 值机订单ID
     * @return 值机订单
     * */
    //TODO 缺少条件：部门名称、服务来源；
    @GetMapping("/orderlist")
    public AjaxResult selectOrderBoardingByorderboardingId(Long orderboardingId){
        return AjaxResult.success(orderBoardingService.selectOrderBoardingByOrderboardingId(orderboardingId));
    }

    /**
     * 值机订单   查看详情（附加备注保存）
     * @param orderboardingId  值机订单ID
     * @param remark  备注
     * @return 结果
     * */
    @PostMapping(value = "/updateOrderBoardingByOrderBoarding")
    public AjaxResult updateOrderBoardingByOrderBoarding(@RequestParam("orderboardingId") Long orderboardingId,
                                                         @RequestParam("remark") String remark){
        return toAjax(orderitemBoardingService.updateOrderBoardingByOrderBoarding(orderboardingId,remark));
    }

    /**
     * 值机订单与值机订单详细新增
     * @param orderBoarding  值机对象
     * */
    @PostMapping("/insertOrderBoardingAndOrderitemBoarding")
    public AjaxResult insertOrderBoardingAndOrderitemBoarding(@RequestBody OrderBoarding orderBoarding){
        return toAjax(orderBoardingService.insertOrderBoardingAndOrderitemBoarding(orderBoarding));
    }

    /**
     * 查询会员值机订单
     * */
    @PostMapping("checkOrderList")
    public AjaxResult selectOrderBoardingByCustomer(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Long type = null;
        if (map.get("type") != null && !map.get("type").equals("")){
            type = Long.valueOf(map.get("type").toString());
        }
        String token = request.getHeader("token");
        Customer customer = remoteCustomerService.getLoginCustomerByToken(token).getData();

        List<OrderBoarding> orderBoardingList = orderBoardingService.selectOrderBoardingByCustomer(customer, type);
        if (orderBoardingList != null && orderBoardingList.size() > 0){
            return AjaxResult.success(orderBoardingList);
        }
        else {
            return null;
        }
    }

    /**
     * 查询登机牌票量数据
     * */
    @PostMapping("/selectOrderBoardingNum")
    public AjaxResult selectOrderBoardingNum(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long customerId = remoteCustomerService.getLoginCustomerByToken(token).getData().getCustomerId();

        BoardingPassVo boardingPassVo;
        try {
            boardingPassVo = orderBoardingService.selectOrderBoardingNum(customerId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(boardingPassVo);
    }

    /**
     * 查询登机牌票量数据(远)
     * */
    @PostMapping("/orderBoardingNum")
    public BoardingPassVo orderBoardingNum(@RequestBody Map<String, Object> map) {
        Long customerId = Long.valueOf(map.get("customerId").toString());

        BoardingPassVo boardingPassVo;
        try {
            boardingPassVo = orderBoardingService.selectOrderBoardingNum(customerId);
        } catch (Exception e){
            return null;
        }

        return boardingPassVo;
    }

    /**
     * 值机选座
     * */
    @PostMapping("/addOrderBoarding")
    public AjaxResult addOrderBoarding(HttpServletRequest request, @RequestBody OrderBoardingVo orderBoardingVo){
        orderBoardingVo.setType("Crm");
        if (orderBoardingVo.getCustomerId() == null || orderBoardingVo.getCustomerId() == 0)
        {
            String token = request.getHeader("token");
            Customer customer = remoteCustomerService.getLoginCustomerByToken(token).getData();
            if (customer != null)
            {
                orderBoardingVo.setType("App");
                orderBoardingVo.setCustomerId(customer.getCustomerId());
                orderBoardingVo.setCustomerName(customer.getCustomerName());
            }
        }

        if (orderBoardingVo != null && orderBoardingVo.getFlightNumber() != null){
            int i;
            try {
                i = orderBoardingService.addOrderBoarding(orderBoardingVo);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (i > 0){
                return AjaxResult.success("200", "选座成功");
            }
            else {
                return AjaxResult.error("500", "选座失败");
            }
        }
        else {
            return AjaxResult.error("500", "缺少必要参数");
        }
    }

    /**
     * 登机牌订单列表
     * */
    @PostMapping("/selectVipchannelOrederList")
    public TableDataInfo selectVipchannelOrederList(@RequestBody VipchannelOrederParameterVo vipchannelOrederParameterVo){
        startPage();
        List<OrderBoarding> orderBoardingList = orderBoardingService.selectVipchannelOrederList(vipchannelOrederParameterVo);
        return getDataTable(orderBoardingList);
    }

    /**
     * 登机牌订单列表
     * */
    @PostMapping("/orederList")
    public List<OrderBoarding> orederList(@RequestBody VipchannelOrederParameterVo vipchannelOrederParameterVo){
        List<OrderBoarding> orderBoardingList = orderBoardingService.selectVipchannelOrederList(vipchannelOrederParameterVo);
        return orderBoardingList;
    }

    /**
     * 登机牌订单详细
     * */
    @PostMapping("/selectBoarding")
    public AjaxResult selectBoarding(@RequestBody Map<String, Object> map) {
        Long orderId = null;
        String orderNum = null;
        if (map.get("orderboardingId") != null && !map.get("orderboardingId").equals("")){
            orderId = Long.valueOf(map.get("orderboardingId").toString());
        }
        else if (map.get("orderNum") != null && !map.get("orderNum").equals("")) {
            orderNum = map.get("orderNum").toString();
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }

        OrderViphallDetailsVo orderViphallDetailsVo;
        try {
            orderViphallDetailsVo = orderBoardingService.selectBoarding(orderId, orderNum);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (orderViphallDetailsVo != null){
            return AjaxResult.success(orderViphallDetailsVo);
        }
        else {
            return AjaxResult.error("未查到数据");
        }
    }

    /**
     * 行程值机订单
     * */
    @PostMapping("/selectOrderBoardingDetailed")
    public JourneyOrderBoardingVo selectOrderBoardingDetailed(@RequestBody Map<String, Object> map) {
        Long orderboardingId = Long.valueOf(map.get("orderboardingId").toString());
        JourneyOrderBoardingVo journeyOrderBoardingVo = orderBoardingService.selectOrderBoardingDetailed(orderboardingId);
        return journeyOrderBoardingVo;
    }

    /**
     * 值机退单
     * */
    @PostMapping("/chargebackOrderBoarding")
    public AjaxResult chargebackOrderBoarding(@RequestBody Map<String, Object> map){
        if (map.get("orderboardingId") != null && !map.get("orderboardingId").equals("")){
            Long orderboardingId = Long.valueOf(map.get("orderboardingId").toString());
            int i = orderBoardingService.chargebackOrderBoarding(orderboardingId);
            if (i > 0){
                return AjaxResult.success("退款成功");
            }
            else {
                return AjaxResult.error("退款失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 查询会员下有多少笔值机订单
     */
    @PostMapping("/queryVipBoardingOrderNumber")
    public int queryVipBoardingOrderNumber(@RequestBody Long customerId){
        return orderBoardingService.queryVipBoardingOrderNumber(customerId);
    }

    /**
     * 订单号获取值机订单信息
     * */
    @PostMapping("/orderNum")
    public OrderBoarding orderBoarding(@RequestBody Map<String, Object> map){
        String orderNum = map.get("orderNum").toString();
        OrderBoarding orderBoarding = orderBoardingService.orderBoarding(orderNum);
        return orderBoarding;
    }
}
